<?php

require_once(Yii::getPathOfAlias('webroot')."/setup.php");

class ShopController extends Controller
{
	/*public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}*/
	public function allowedActions()
	{
		return '*';
	}
	
	public function actionIndex()
	{
		
		$cateThemes = CategoriesTheme::model()->getDataCategories();
		
		$criteria = new CDBCriteria;
		$criteria->select = "id, title";
		$provinces = Provinces::model()->findAll($criteria);
		
		$ward = new Wards;
		$wards = $ward->getWards(57);

		$this->render("index", array(
			'cateThemes' => $cateThemes,
			'provinces' => $provinces,
			'wards' => $wards,
		));
	}
	
	public function actionGetWards($id){
		$ward = new Wards;
		$wards = $ward->getWards1($id);
		echo json_encode($wards);
	}
	
	public function actionRegisterTheme(){
		if(!empty($_POST)){
			$model = new Shop;
			$model->attributes = $_POST;
			$model->pass = $_POST['pass'];
			$model->city = $_POST['provinces'];
			$model->district = $_POST['wards'];
			$model->id_category_theme = $_POST['cateTheme'];
			$model->domain = str_replace(" ", "", trim($_POST['domain']));
			$model->created =  time();
			
			if($model->save()){
				echo $model->id;
			}else	echo false;
		}
	}
	
	public function actionChooseTheme($shop_id){
		
		
		$cateTheme = Shop::model()->find($shop_id)->id_category_theme;
		$criteria = new CDBCriteria;
		if(!empty($cateTheme))
			$criteria->addCondition("id_category_theme = {$cateTheme}");

		$themes = Themes::model()->findAll($criteria);

		
		$this->render("chooseTheme", array(
			'themes' => $themes,
		));
	}
	
	public function actionCreateWeb($id_shop =null, $id_theme=null){
		
		$shop = Shop::model()->findByPK($id_shop);
		
		$domain = $shop->domain;
		$pass = $shop->pass;
		$alias_cate = CategoriesTheme::model()->findByPK($shop->id_category_theme)->alias_folder;
		$alias_theme = Themes::model()->findByPK($id_theme)->alias_folder;

		$installShop = new installShop;
		// create folder contain source 
		$installShop->createFolder($domain);
		
		// extract file shop.zip to folder source 
		$installShop->shopDirectory = $installShop->shopDirectory.$alias_cate.DIRECTORY_SEPARATOR;
		$installShop->parentDirectory = $installShop->parentDirectory.$domain.DIRECTORY_SEPARATOR;
		$installShop->extractShop();
		
		// Delete all folder in dir catalog/theme
		$installShop->parentDirectory = $installShop->parentDirectory."catalog".DIRECTORY_SEPARATOR."view".DIRECTORY_SEPARATOR."theme".DIRECTORY_SEPARATOR;
		$installShop->deleteFolder($installShop->parentDirectory);
		
		// Copy folder theme from dir shop to dir web
		$installShop->shopDirectory = $installShop->shopDirectory."theme".DIRECTORY_SEPARATOR;
		$installShop->parentDirectory = $installShop->parentDirectory.$domain.DIRECTORY_SEPARATOR;
		$installShop->copyShop($alias_cate."_".$alias_theme);
		
		// create database
		$installShop->shopDirectory = $installShop->dirDefault."shop".DIRECTORY_SEPARATOR.$alias_cate.DIRECTORY_SEPARATOR;
		$installShop->processDatabase("web_".$domain, $alias_cate."_".$alias_theme, $domain, $pass);
		
		// create 2 file config
		$installShop->shopFolderName = $domain;
		$installShop->destinationDirectory = $installShop->destinationDirectory;
		$installShop->createConfigFile("web_".$domain);
			
		$shop->is_create_web = 1;
		$shop->save();
	}
	
	
	
	
	
	
	
	
	
	
}